<?php

use DI\Container;

require_once __DIR__ . "/vendor/autoload.php";

class MyDB
{
    private $db;//这里可能是pdo 可能是别的，仅仅为了演示

    public function __construct($connInfo)
    {
        //略
    }

    public function queryForRows($sql)
    {
        return ['user_id' => 101, "user_name" => "shenyi"];
    }
}

class MyUser
{//老大
    private $mydb;

    public function __construct(MyDB $DB)
    {
        $this->mydb = $DB;
        //$this->mydb=new MyDB($dsn);//主动实例化DB
    }

    public function getAllUsers(): array
    {//业务方法
        return $this->mydb->queryForRows("select * from users");
    }
}


$container = new DI\Container();
$container->set("mydb", function () {
    return new MyDB("xxxoo");
});
$container->set("myuser", function (Container $c) {
    return new MyUser($c->get("mydb"));
});
$myuser = $container->get("myuser");
var_dump($myuser->getAllUsers());

